package com.xiaoyong.goods.controller;



import com.github.pagehelper.Page;
import com.xiaoyong.entity.PageResult;
import com.xiaoyong.entity.Result;
import com.xiaoyong.entity.StatusCode;
import com.xiaoyong.goods.pojo.Brand;
import com.xiaoyong.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping//查询所有
    public Result findAll() {
        List<Brand> brandList = brandService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", brandList);
    }

    @GetMapping(value = "/{id}")//根据id查询
    public Result findById(@PathVariable Integer id) {
        Brand byId = brandService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", byId);
    }

    @PostMapping//添加
    public Result add(@RequestBody Brand brand) {
        brandService.add(brand);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    @PutMapping(value = "/{id}")//修改
    public Result update(@RequestBody Brand brand, @PathVariable Integer id) {
        brand.setId(id);
        brandService.update(brand);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    @DeleteMapping(value = "/{id}")//删除
    public Result delete(@PathVariable Integer id) {
        brandService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @GetMapping(value = "/search")//多条件搜索品牌数据
    public Result findList(@RequestParam Map searchMap) {
        List list = brandService.findList(searchMap);
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    @GetMapping(value = "/search/{page}/{size}")//分页搜索实现
    public Result findPage(@PathVariable int page, @PathVariable int size) {
        Page<Brand> pageList = brandService.findPage(page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    @GetMapping(value = "/searchPage/{page}/{size}")//分页搜索实现
    public Result findPage(@RequestParam Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page pageList = brandService.findPage(searchMap, page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }


    @GetMapping("/category/{category}")// 根据分类名称查询品牌列表
    public Result findListByCategoryName(@PathVariable String category){
        System.out.println(category);
        List<Map> categoryName = brandService.findListByCategoryName(category);
        return new Result(true, StatusCode.OK, "查询成功", categoryName);
    }


//    @GetMapping("/category/{category}")//根据商品分类名称查询规格列表
//    public Result findSpecListByCategoryName(@PathVariable String category){
//        List<Map> categoryName = brandService.findListByCategoryName(category);
//        return new Result(true,StatusCode.OK,"",categoryName);
//
//    }


}
